1 00:00:00,110 --> 00:00:05,480 In this lecture, we're going to take a look at a service called the Messaging Service. 2 00:00:05,480 --> 00:00:09,860 This is a very simple service that allows you to send messages between servers. 3 00:00:09,860 --> 00:00:12,290 And it only has two functions. 4 00:00:12,290 --> 00:00:18,680 One of these functions is to listen to a particular topic and receive messages from it. 5 00:00:18,680 --> 00:00:23,990 It's called subscribe async and it says this function registers a callback to begin listening to the 6 00:00:23,990 --> 00:00:24,980 given topic. 7 00:00:24,980 --> 00:00:28,730 The callback is invoked when a topic receives a message. 8 00:00:28,730 --> 00:00:34,910 It can be called multiple times for the same topic, and it says the callback is invoked with a single 9 00:00:34,910 --> 00:00:35,390 argument. 10 00:00:35,390 --> 00:00:37,940 A table with the following two entries. 11 00:00:37,940 --> 00:00:44,120 It has a key called data and it says developer supplied payload and then sent and it says unit time 12 00:00:44,120 --> 00:00:47,120 in seconds at which the message was sent. 13 00:00:47,120 --> 00:00:51,890 And then the other function we have is the ability to send messages to a particular topic. 14 00:00:51,890 --> 00:00:57,470 So this one's called publish async and it says this function sends the provided message to all subscribers 15 00:00:57,470 --> 00:01:01,700 to the topic triggering their register callbacks to be invoked. 16 00:01:02,030 --> 00:01:07,040 So the messaging service allows servers of the same experience to communicate with each other in real 17 00:01:07,040 --> 00:01:09,980 time, less than one second using topics. 18 00:01:09,980 --> 00:01:16,070 Topics are developer defined strings 1 to 80 characters that serve as used to send and receive messages. 19 00:01:16,070 --> 00:01:18,710 Delivery is best effort and not guaranteed. 20 00:01:18,710 --> 00:01:23,120 Make sure to architect your experience so delivery failures are not critical. 21 00:01:23,120 --> 00:01:27,170 They also give us some information about the different limitations with messages. 22 00:01:27,170 --> 00:01:32,750 For example, what the maximum size of a message can be, how many messages you can send per minute. 23 00:01:32,750 --> 00:01:38,150 How many messages can be received per minute per topic, how many messages can be received for an entire 24 00:01:38,150 --> 00:01:44,720 game, the subscriptions allowed per game server and subscribe requests per game server. 25 00:01:44,990 --> 00:01:49,010 There are also some other limitations with the messaging service that we're going to take a look at 26 00:01:49,010 --> 00:01:55,220 in a moment, but basically, the service, as it says, allows you to send information between different 27 00:01:55,220 --> 00:01:55,580 servers. 28 00:01:55,580 --> 00:01:59,150 This is how you communicate or have cross server messaging. 29 00:01:59,510 --> 00:02:02,210 So inside of studio I've made a new place. 30 00:02:02,210 --> 00:02:07,670 This is just called my messaging service demonstration, and I've published it to Roblox and I've made 31 00:02:07,670 --> 00:02:09,020 the server count only one. 32 00:02:09,020 --> 00:02:13,010 So that way I can have two separate accounts hopping into two separate servers. 33 00:02:13,010 --> 00:02:16,310 So that way we can see the messages being sent between them in action. 34 00:02:16,310 --> 00:02:18,560 I have a script and server script service. 35 00:02:18,560 --> 00:02:20,270 I'll just call it messaging service. 36 00:02:20,270 --> 00:02:23,480 And let's go ahead and grab the messaging service. 37 00:02:23,480 --> 00:02:26,390 So it's going to be game git service messaging service. 38 00:02:26,390 --> 00:02:30,290 And we can go ahead and take a look at the two functions that it has. 39 00:02:30,290 --> 00:02:34,070 Now the first function we're going to take a look at is subscribe. 40 00:02:34,070 --> 00:02:36,860 Async begins listening to a given topic. 41 00:02:36,860 --> 00:02:39,740 So we need to define whatever topic we would like. 42 00:02:39,740 --> 00:02:46,490 For this I'm just going to call it my player message topic because I want it to receive information. 43 00:02:46,490 --> 00:02:49,340 When a player my game sends a chat message. 44 00:02:49,340 --> 00:02:52,640 So we're going to call it player message, but you can call it whatever you'd like. 45 00:02:52,790 --> 00:02:54,950 And then we have to give it a callback function. 46 00:02:54,950 --> 00:02:56,690 So we'll pass a lambda function here. 47 00:02:56,690 --> 00:03:00,770 And if you remember the documentation this function is given an argument. 48 00:03:00,770 --> 00:03:03,050 That's a table that contains two keys. 49 00:03:03,050 --> 00:03:05,270 One is called data and one is called sent. 50 00:03:05,270 --> 00:03:11,720 So I will just call this my um I guess I'll just call this my message and I'll type annotate it as a 51 00:03:11,720 --> 00:03:14,060 table that has a key of data. 52 00:03:14,060 --> 00:03:15,470 And this could be anything. 53 00:03:15,470 --> 00:03:19,280 And then it also has a key called sent which is a Unix timestamp. 54 00:03:19,280 --> 00:03:20,870 So that's going to be a number. 55 00:03:21,470 --> 00:03:26,660 And now what I want to do is I want to be able to print what the message is inside of the console. 56 00:03:26,660 --> 00:03:34,040 So I'm going to print whenever this callback is invoked I'm going to print that we received a message. 57 00:03:34,880 --> 00:03:39,260 And that message is going to be inside of our message table. 58 00:03:39,560 --> 00:03:44,120 And we're going to print data as well as when this message was sent. 59 00:03:44,740 --> 00:03:47,050 And now to send messages to different servers. 60 00:03:47,050 --> 00:03:50,230 What I'm going to do is I'm also going to grab the player service. 61 00:03:52,580 --> 00:03:58,910 And I want to go ahead and listen for when a player is added into my game, because I want to listen 62 00:03:58,910 --> 00:04:00,920 for when this player chats. 63 00:04:01,910 --> 00:04:04,010 So we're going to do player dot chatted. 64 00:04:04,880 --> 00:04:06,620 We're going to connect a function. 65 00:04:06,620 --> 00:04:08,570 We're going to get that message. 66 00:04:08,570 --> 00:04:13,730 And to demonstrate some of the limitations with the messaging service, we're going to have some predefined 67 00:04:13,730 --> 00:04:14,540 messages here. 68 00:04:14,540 --> 00:04:20,780 So for example if let's say our player messages, let's say they say something like, um, I kind of 69 00:04:20,780 --> 00:04:21,800 want this to be like command. 70 00:04:21,800 --> 00:04:23,480 So we could do exclamation mark. 71 00:04:23,480 --> 00:04:25,850 And the first example we'll do here is mixed. 72 00:04:25,850 --> 00:04:33,440 So what I'm going to do when a player chats this is I'm going to try and attempt to send a mixed table 73 00:04:33,440 --> 00:04:34,640 through the messaging service. 74 00:04:34,640 --> 00:04:38,060 So we're going to do messaging service publish async. 75 00:04:38,060 --> 00:04:41,540 We need to refer to what topic we want to publish this message to. 76 00:04:41,540 --> 00:04:43,910 In this case that's our player message topic. 77 00:04:43,910 --> 00:04:45,380 So we'll copy that there. 78 00:04:45,380 --> 00:04:51,110 And the message that I want to send is I want to send a mixed table or a table that has both indexes 79 00:04:51,110 --> 00:04:52,610 and keys inside of it. 80 00:04:52,610 --> 00:04:55,430 So inside of this table I'll have an index of one. 81 00:04:55,430 --> 00:04:58,460 We could set that to something like a string of hello. 82 00:04:58,460 --> 00:05:00,230 But then I also want to have keys. 83 00:05:00,230 --> 00:05:01,760 So we'll have key one. 84 00:05:02,210 --> 00:05:06,920 We'll have this one say howdy and then we'll have key number two. 85 00:05:07,490 --> 00:05:09,560 And this one will be called hi. 86 00:05:10,070 --> 00:05:16,610 Now if a player sends a different message let's say they say, um, let's try to pass a function through 87 00:05:16,610 --> 00:05:17,840 the messaging service. 88 00:05:17,840 --> 00:05:19,670 So we could do messaging service. 89 00:05:19,670 --> 00:05:22,250 And actually let's just copy what we've done here. 90 00:05:22,250 --> 00:05:23,600 Paste this here. 91 00:05:24,260 --> 00:05:28,910 And then we're going to try to pass a function through the messaging service. 92 00:05:28,910 --> 00:05:33,380 This will just be some kind of simple, uh, lambda function will pass through here. 93 00:05:33,380 --> 00:05:34,700 We'll see if that works. 94 00:05:36,200 --> 00:05:37,940 Um, what else do we want to do? 95 00:05:37,940 --> 00:05:42,050 Let's go ahead and try to see if we can pass a meta table through the messaging service. 96 00:05:42,050 --> 00:05:44,660 So we'll do we'll call this one meta table. 97 00:05:46,130 --> 00:05:49,970 And again we'll just copy this paste this here. 98 00:05:50,490 --> 00:05:54,900 And we'll call the set meta table function to return a new table. 99 00:05:54,900 --> 00:05:57,120 So we'll just have an empty table. 100 00:05:57,120 --> 00:06:00,630 Actually let's put some keys in and we'll call this key one is equal to. 101 00:06:00,630 --> 00:06:01,800 We'll just say meta. 102 00:06:02,250 --> 00:06:06,780 And then this one key two we'll just say hahaha whatever. 103 00:06:06,780 --> 00:06:09,450 And then we want to attach a meta table to this. 104 00:06:09,450 --> 00:06:11,910 And this meta table is just going to have some kind of key in it. 105 00:06:11,910 --> 00:06:14,190 We'll just call it uh key two. 106 00:06:14,190 --> 00:06:16,770 And we'll just say table something like that. 107 00:06:16,770 --> 00:06:22,920 So let's go ahead and try to pass a table with a meta table attached to it through the messaging service. 108 00:06:23,220 --> 00:06:24,690 What else should we try. 109 00:06:25,740 --> 00:06:27,390 Um, let's do it for the other data types. 110 00:06:27,390 --> 00:06:29,460 So let's do it for like strings. 111 00:06:29,610 --> 00:06:32,460 We should be able to send strings through the messaging service. 112 00:06:32,460 --> 00:06:35,430 So again I'm just going to copy this paste this here. 113 00:06:35,910 --> 00:06:38,880 And let's go ahead and send a message of like hello there. 114 00:06:40,290 --> 00:06:45,480 Let's check to see if the player says uh they want to send a number. 115 00:06:47,800 --> 00:06:49,240 So we'll copy this, paste this here. 116 00:06:49,240 --> 00:06:51,190 We'll just pass a number like one two, three. 117 00:06:51,190 --> 00:06:57,370 And then finally let's go ahead and try to send a boolean to the messaging service. 118 00:06:57,760 --> 00:07:02,770 So we'll just copy this paste this there and we'll send the value of true. 119 00:07:03,680 --> 00:07:09,950 Now of course, the subscribe async and the publish async are asynchronous functions, meaning they 120 00:07:09,950 --> 00:07:11,900 have the chance to error. 121 00:07:11,900 --> 00:07:18,440 So you're more than likely going to want to wrap these function calls inside of a p call or a protected 122 00:07:18,440 --> 00:07:18,980 call. 123 00:07:18,980 --> 00:07:22,100 I'm not going to do it here just because I'm not going to waste my time with that. 124 00:07:22,100 --> 00:07:26,360 This is just a demonstration, but let's go ahead and publish these changes. 125 00:07:26,360 --> 00:07:31,040 We'll publish this to Roblox, and then I'm going to hop into this game with two different accounts, 126 00:07:31,040 --> 00:07:34,040 so we can see this messaging service in action. 127 00:07:34,370 --> 00:07:41,510 And actually before I do that, um, I need to account for the fact that when we print messages out, 128 00:07:41,510 --> 00:07:46,130 like, for example, this is a table, it's only going to print the location of the table in memory 129 00:07:46,130 --> 00:07:51,770 inside of the console, because when you're doing it in studio, studio gives it some nice extra formatting 130 00:07:51,770 --> 00:07:53,840 so you can actually see what's inside of the table. 131 00:07:53,840 --> 00:07:57,770 But unfortunately, when we're actually in the Roblox player using the developer console, it's not 132 00:07:57,770 --> 00:07:59,750 going to show up that way. 133 00:07:59,750 --> 00:08:03,830 So if the type of the value of this message we receive. 134 00:08:03,830 --> 00:08:10,130 So we could do if message dot data, if the type of that is a table, then let's just go ahead and loop 135 00:08:10,130 --> 00:08:13,220 through every single key value pair inside of the table. 136 00:08:13,220 --> 00:08:17,090 So in pairs message dot data. 137 00:08:17,090 --> 00:08:20,600 This will work for both key value pairs and index value pairs. 138 00:08:20,600 --> 00:08:21,860 It'll just print everything. 139 00:08:21,860 --> 00:08:26,000 And then we can just go ahead and print out the key and the value. 140 00:08:26,030 --> 00:08:31,820 Now I don't think that we'll be able to send a mixed tables through the messaging service. 141 00:08:31,820 --> 00:08:36,890 And just to confirm this, let's go ahead and print out inside of this table to see if it has a key 142 00:08:36,920 --> 00:08:37,550 one. 143 00:08:37,640 --> 00:08:42,590 And also let's check to see if this table has a key number two. 144 00:08:42,860 --> 00:08:45,890 And then to check to see if this table has a meta table. 145 00:08:45,890 --> 00:08:50,870 We can just print something like um get meta table. 146 00:08:50,870 --> 00:08:55,100 We'll get the meta table for this particular table. 147 00:08:55,100 --> 00:09:00,950 And we're only going to do that if this table has that key one inside of it of meta. 148 00:09:00,950 --> 00:09:05,720 So this way we can tell if we actually had the table passed to us that contained the meta table, if 149 00:09:05,720 --> 00:09:08,330 it has uh, this value stored in key one. 150 00:09:08,330 --> 00:09:14,540 So if message dot data key one, if that's equal to meta, then we know this is supposed to be the table 151 00:09:14,540 --> 00:09:16,100 that has the meta table attached to it. 152 00:09:16,100 --> 00:09:21,530 So we'll print to see if it actually has a meta table after being passed through the messaging service. 153 00:09:21,530 --> 00:09:21,980 Okay. 154 00:09:21,980 --> 00:09:23,390 Let's publish those changes. 155 00:09:23,390 --> 00:09:27,170 And now let's go ahead and hop into the Roblox player and test it out. 156 00:09:27,680 --> 00:09:31,700 Okay, so here I am in my game on two separate accounts. 157 00:09:31,700 --> 00:09:37,610 And what I'm going to do is I'm going to open up the F9 menu, and we're going to go to the server side 158 00:09:37,610 --> 00:09:40,520 of the console, and we're going to do the same thing for my other account. 159 00:09:40,520 --> 00:09:45,830 And let's go ahead and see if we are able to receive messages on this other server. 160 00:09:45,830 --> 00:09:51,200 So the first command we could go ahead and try out is let's go ahead and try out the string command. 161 00:09:51,200 --> 00:09:52,790 So we're going to chat string. 162 00:09:54,060 --> 00:09:59,880 And as you can see, both servers receive the message of hello there and it gives us the timestamp of 163 00:09:59,880 --> 00:10:02,490 when that message was sent on both of the servers. 164 00:10:02,490 --> 00:10:08,310 So you can see that both of them, since they were both listening to that exact same topic, which was 165 00:10:08,310 --> 00:10:12,240 the player message topic, they both received the message of hello there! 166 00:10:12,240 --> 00:10:13,380 Very cool. 167 00:10:13,380 --> 00:10:16,230 Now let's go ahead and try it out with a number. 168 00:10:16,230 --> 00:10:18,060 We'll do exclamation mark number. 169 00:10:18,270 --> 00:10:19,110 And there you go. 170 00:10:19,110 --> 00:10:25,320 Both servers receive that message and the data was indeed one, two, three for both servers. 171 00:10:26,010 --> 00:10:26,400 Okay. 172 00:10:26,400 --> 00:10:29,580 Let's go ahead and try out sending a boolean. 173 00:10:29,580 --> 00:10:30,990 So we'll do a boolean. 174 00:10:30,990 --> 00:10:31,770 Hey look at that. 175 00:10:31,770 --> 00:10:36,570 We received the message on both servers as well which was the value of true. 176 00:10:36,870 --> 00:10:39,270 Okay let's get a little bit more fancy. 177 00:10:39,270 --> 00:10:41,550 Let's go ahead and try sending a mixed table. 178 00:10:41,550 --> 00:10:44,160 So we're going to do exclamation mark mixed. 179 00:10:44,610 --> 00:10:46,710 And what are you going to see. 180 00:10:46,710 --> 00:10:49,230 Well it did receive a table. 181 00:10:49,230 --> 00:10:55,080 But if you take a look at that it only got that first index value pair in the table which was the index 182 00:10:55,080 --> 00:11:00,090 of one and the value of hello with it, meaning those other two keys in the table, key one and key 183 00:11:00,090 --> 00:11:02,130 two did not exist. 184 00:11:02,130 --> 00:11:05,130 So this is one of the limitations with the messaging service. 185 00:11:05,130 --> 00:11:10,050 You cannot send mixed tables through the publish async function. 186 00:11:10,170 --> 00:11:17,160 Let's go ahead and try using let's say um let's try to send a meta table through the messaging service. 187 00:11:17,160 --> 00:11:23,700 So if we do exclamation mark meta table you're going to see that we do get the keys for the table printed 188 00:11:23,700 --> 00:11:24,030 out. 189 00:11:24,030 --> 00:11:29,550 But if you notice when we try to print out the meta table itself we got nil. 190 00:11:29,550 --> 00:11:32,400 So that's another limitation with the messaging service. 191 00:11:32,400 --> 00:11:37,530 Any tables that have meta tables attached to them those meta tables will be lost. 192 00:11:37,530 --> 00:11:43,770 So this is basically the same thing as losing, um, meta tables through remote events and losing mixed 193 00:11:43,770 --> 00:11:45,450 table data through remote events. 194 00:11:45,450 --> 00:11:49,530 You can't send them through remote events and you can't send them through the messaging service either. 195 00:11:49,650 --> 00:11:51,570 Let's go ahead and try a function. 196 00:11:51,570 --> 00:11:53,970 Let's send a function through the messaging service. 197 00:11:53,970 --> 00:12:00,510 And we're going to see is we get an error cannot publish function, can only accept valid UTF eight 198 00:12:00,510 --> 00:12:01,140 characters. 199 00:12:01,140 --> 00:12:05,460 So as you can see we cannot send functions through the messaging service either. 200 00:12:05,460 --> 00:12:07,770 This one actually caused an error. 201 00:12:08,010 --> 00:12:11,520 And that's basically all there is to the messaging service. 202 00:12:11,520 --> 00:12:15,000 It's a very simple service that only has two functions. 203 00:12:15,000 --> 00:12:22,230 One of the functions listens to a particular topic and is given a table that contains the data for that 204 00:12:22,230 --> 00:12:23,100 particular topic. 205 00:12:23,100 --> 00:12:27,030 When we use the publish async function and when that message was sent. 206 00:12:27,030 --> 00:12:32,460 And then of course we use the publish async message to send a message to a particular topic, whatever 207 00:12:32,460 --> 00:12:37,740 values we want, as long as they aren't functions, we'll lose meta tables through the publishing service, 208 00:12:37,740 --> 00:12:41,310 and we can't send mixed tables through the messaging service either. 209 00:12:41,310 --> 00:12:46,830 And of course, you can't send other stuff like custom Roblox data types or different things like for 210 00:12:46,830 --> 00:12:51,150 example, you can't send instances through the messaging service. 211 00:12:51,660 --> 00:12:56,700 And actually, just to have a little fun, let's go ahead and try to send some kind of Roblox data type 212 00:12:56,700 --> 00:12:57,870 through the messaging service. 213 00:12:57,870 --> 00:13:03,180 Like, let's try to send, uh, a color, a color three data type, and let's go ahead and see what 214 00:13:03,180 --> 00:13:03,570 happens. 215 00:13:03,570 --> 00:13:07,530 We're going to publish it to player message and we'll just pass a new color. 216 00:13:07,530 --> 00:13:09,780 Three let's just do a white color. 217 00:13:09,780 --> 00:13:15,600 And let's go ahead and see what happens when we try to publish this through the messaging service. 218 00:13:15,600 --> 00:13:17,220 So I'll publish changes. 219 00:13:18,790 --> 00:13:22,540 All right, we'll do exclamation mark color, and we should. 220 00:13:22,540 --> 00:13:22,990 There we go. 221 00:13:22,990 --> 00:13:24,040 We get the same error. 222 00:13:24,040 --> 00:13:27,490 Cannot publish color three can only accept valid UTF eight characters. 223 00:13:27,490 --> 00:13:32,860 So you are not able to send custom Roblox data types through the messaging service either. 224 00:13:33,810 --> 00:13:39,240 Hopefully this lecture has given you a better understanding of the messaging service and how to use 225 00:13:39,240 --> 00:13:40,560 its two functions. 226 00:13:40,590 --> 00:13:43,320 This should open up many possibilities in your games. 227 00:13:43,320 --> 00:13:48,450 For example, maybe you could create global announcements across multiple servers in your game. 228 00:13:48,450 --> 00:13:54,420 You could create some kind of real time server browser, or even some kind of cross server chat, where 229 00:13:54,420 --> 00:13:58,890 other players can chat with other players that are in completely separate servers. 230 00:13:58,890 --> 00:14:01,680 Thank you for watching and I'll see you in the next lecture.